
package com.woniu.job.controller;

import com.woniu.job.entity.Category;
import com.woniu.job.entity.Company;
import com.woniu.job.entity.Work;
import com.woniu.job.entity.vo.ResultVO;
import com.woniu.job.service.ICategoryService;
import com.woniu.job.service.IWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/job")
public class JobController {
    @Autowired
    private ICategoryService iCategoryService;
    @Autowired
    private IWorkService iWorkService;

    @GetMapping("/add")
    public String add(Model model) {
        List<Category> categories = iCategoryService.getAll();
        model.addAttribute("categories", categories);
        return "company/job_add";
    }

    @GetMapping("/manage/{p}")
    public String manage(Model model, @PathVariable Integer p,@SessionAttribute Company company) {

        model.addAttribute("iPage", iWorkService.getByPage(p,company.getId()));
        return "company/job_manage";
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResultVO list(@SessionAttribute Company company) {
        List<Work> works = iWorkService.getWorksById(company.getId());

        return new ResultVO(works);
    }

    @PostMapping("/add")
    public String add(Work work, @SessionAttribute Company company) {
        work.setCompanyId(company.getId());
        iWorkService.add(work);
        return "company/job_add";
    }

    @ResponseBody
    @GetMapping("/refresh/{id}")
    public String refresh(@PathVariable String id) {
        iWorkService.refresh(id);
        return "更新成功";
    }

    @ResponseBody
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable String id) {
        iWorkService.delete(id);
        return "删除成功";
    }

    @ResponseBody
    @PostMapping("/refresh2")
    public String refreshAll(String ids) {
     String[] x=ids.split(",");
     for(String c:x){
        iWorkService.refresh(c);
     }
        return "更新成功";
    }

    @ResponseBody
    @PostMapping("/delete2")
    public String delete2(String ids) {
        String[] x=ids.split(",");
        for(String c:x){
            iWorkService.delete(c);
        }
        return "更新成功";
    }
}